//
//  UserViewController.m
//  YiShangPin
//
//  Created by 衣尚品1 on 15/3/5.
//  Copyright (c) 2015年 yishangpin2. All rights reserved.
//

#import "UserViewController.h"
#import "UserSmallView.h"
#import "AppDelegate.h"

@interface UserViewController (){
    UIImageView *tabbarImage;
    UserSmallView *userSmallView;
    AppDelegate *delegate;
}


@end

@implementation UserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    delegate = [[UIApplication sharedApplication] delegate];
    //添加滑动小箭头
    tabbarImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.itemsButton.frame.origin.x + self.itemsButton.frame.size.width/2 - 8, self.itemsButton.frame.origin.y + self.itemsButton.frame.size.height + 43, 15, 15)];
    tabbarImage.image = [UIImage imageNamed:@"MHTabBarIndicator"];
    [self.view insertSubview:tabbarImage atIndex:3];
    
    [self.itemsButton setTitle:[NSString stringWithFormat:@"%d",delegate.CollectArray.count] forState:0];
    
    self.noFollowImage.hidden = YES;
    self.noFollowLabel.hidden = YES;
}

- (void)viewWillAppear:(BOOL)animated{
    [self upload];
    self.navigationItem.title = @"YiShangPin";
    if (tabbarImage.frame.origin.x > self.itemsButton.frame.size.width + self.itemsButton.frame.origin.x) {
        userSmallView.hidden = YES;
    }
    [self.itemsButton setTitle:[NSString stringWithFormat:@"%d",delegate.CollectArray.count] forState:0];
}

- (void)viewDidDisappear:(BOOL)animated{
    [userSmallView removeFromSuperview];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


- (void)upload{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"UserSmallView" owner:self options:nil];
    userSmallView = nib[0];
    userSmallView.frame = CGRectMake(0, 5, self.view.frame.size.width/2, userSmallView.BigImage.frame.size.height+self.view.frame.size.height/6.5);
    [self.scrollview addSubview:userSmallView];
}

- (IBAction)clickItensButton:(id)sender {
    //平移箭头，0.3是移动速度
    [UIView animateWithDuration:0.2 animations:^{
        [tabbarImage setTransform:(CGAffineTransformMakeTranslation(0, 0))];
    }];
    userSmallView.hidden = NO;
    self.noFollowLabel.hidden = YES;
    self.noFollowImage.hidden = YES;
}

- (IBAction)clickFollowingButton:(id)sender {
    [UIView animateWithDuration:0.2 animations:^{
        [tabbarImage setTransform:(CGAffineTransformMakeTranslation(self.followingButton.frame.size.width+8, 0))];
    }];
    userSmallView.hidden = YES;
    self.noFollowLabel.hidden = NO;
    self.noFollowImage.hidden = NO;
    self.noFollowLabel.text = @"You aren't following any users";
}

- (IBAction)clickFollowersButton:(id)sender {
    [UIView animateWithDuration:0.2 animations:^{
        [tabbarImage setTransform:(CGAffineTransformMakeTranslation(self.followingButton.frame.size.width + self.followersButton.frame.size.width +16, 0))];
    }];
    userSmallView.hidden = YES;
    self.noFollowLabel.hidden = NO;
    self.noFollowImage.hidden = NO;
    self.noFollowLabel.text = @"No users are following you";
}

- (IBAction)CompileHeadPic:(id)sender {
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
    [actionSheet addButtonWithTitle:@"Take Photo"];
    [actionSheet addButtonWithTitle:@"Choose Existing"];
    // 同时添加一个取消按钮
    [actionSheet addButtonWithTitle:@"Cancel"];
    // 将取消按钮的index设置成我们刚添加的那个按钮，这样在delegate中就可以知道是那个按钮
    actionSheet.destructiveButtonIndex = actionSheet.numberOfButtons - 1;
    [actionSheet showInView:self.view];
    
}

#pragma mark - 判断设备是否有摄像头

- (BOOL) isCameraAvailable
{
    return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
}


#pragma mark - 选择图片来源

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{
//    if (!self.images) {
//        
//        self.images=[[NSMutableArray alloc]init];
//    }
    
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.editing = YES;
    imagePicker.allowsEditing = YES;
    imagePicker.delegate = self;
    
    if (buttonIndex == 0)//照相机
    {
        if ([self isCameraAvailable]) {
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:imagePicker animated:YES completion:nil];
            //            [self presentModalViewController:imagePicker animated:YES];
        }else{
            
            UIAlertView *aler=[[UIAlertView alloc]initWithTitle:@"提示" message:@"该设备没有摄像头" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [aler show];
        }
    }
    if (buttonIndex == 1)
    {
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:imagePicker animated:YES completion:nil];
        //        [self presentModalViewController:imagePicker animated:YES];
    }
    
    
}

#pragma mark - UIImagePicker delegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    //    [picker dismissModalViewControllerAnimated:YES];
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    [self performSelector:@selector(saveImage:) withObject:image afterDelay:0.5];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    //    [picker popViewControllerAnimated:YES];
    //    [picker dismissModalViewControllerAnimated:YES];
    [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 图片选择完毕后，执行事件

- (void)saveImage:(UIImage *)image
{
    [_HeadPic setBackgroundImage:image forState:0];
    
}
@end
